// Fetch functions
var loading = new Array("Loading ...");
var empty = new Array();
// ****** Get/Put Properties
function Set_ApplID(applID,ret){
	if( !isCB(ret) ){
		return service(function(r){Set_ApplID(applID,r)},"GetCarInfo",applID);
	}
	if( showSOAPError(ret) )return;
	var xmlCI = new ActiveXObject("Msxml2.DOMDocument");
	xmlCI.loadXML(ret.value);
	var row = xmlCI.documentElement.firstChild;
	Set_Year(row.getAttribute("Year"));
}
function Get_Year(){return selectedValue(selYear);}
function Set_Year(v){
	selectByValue(selYear,v);
	getMakes(v);
}
function Get_Make(){return selectedValue(selMake);}
function Set_Make(v){
	var sel = selMake;
	if( isObject(v) )return fillSELECT(sel,v);
	selectByValue(selMake,v);
	if( isSelected(sel) )getModels(Year,v);
	else clearSelections(sel);
}
function Get_Model(){return selectedValue(selModel);}
function Set_Model(v){
	var sel = selModel;
	if( isObject(v) )return fillSELECT(sel,v);
	selectByValue(sel,v);
	if( isSelected(sel) )getSeries(Year,Make,selectedValue(sel));
	else clearSelections(sel);
}
function Get_Series(){return selectedValue(selSeries);}
function Set_Series(v){
	var sel = selSeries;
	if( isObject(v) )return fillSELECT(sel,v);
	selectByValue(sel,v);
	if( isSelected(sel) )getBodyStyles(Year,Make,Model,selectedValue(sel));
	else clearSelections(sel);
}
function Get_BodyStyle(){return selectedValue(selBodyStyle);}
function Set_BodyStyle(v){
	var sel = selBodyStyle;
	if( isObject(v) )return fillSELECT(sel,v);
	selectByValue(sel,v);
	if( isSelected(sel) )getAddOns(Year,Make,Model,Series,selectedValue(sel));
	else clearSelections(sel);
}
function Get_AddOns(){
	var rs = eval(tblAddOns.dataSrc.substr(1)).recordset;
	var values=new Array;
	if( rs.recordCount )
		for( rs.MoveFirst() ; !rs.EOF; rs.MoveNext() )
			values.push(parseInt(rs("Value").value));
	return values;
}
function Get_BookValue(){return parseInt(elmBookValue.innerText);}
function Set_BookValue(v){elmBookValue.innerText = v;}

function Get_State(){return selState[selState.selectedIndex].text;}
function Set_State(v){return selectByValue(selState,v);}

// ***** Get Year,Make,Model,Series,BodyStyle ...
function getYears(ret){
	if( !service )return setTimeout(function(){getYears()},200);
	if( selYear.length )return;
	getAddOns();
	if( !isCB(ret))return service(getYears,"FetchYears");
	if( showSOAPError(ret) )return;
	fillSELECT(selYear,ret.value);
}
function getMakes(year,make,ret){
	var sel = selMake;
	if( !isCB(ret) ){
		var vMake = selectedValue(sel);
		service(function(r){getMakes(year,vMake,r)},"FetchMakes",year);
		return fillSELECT(sel,loading,"");
	}
	if( showSOAPError(ret) )return;
	fillSELECT(sel,ret.value);
	if( make )Make=make;
}
function getModels(year,make,model,ret){
	var sel = selModel;
	if( !isCB(ret) ){
		var vModel = selectedValue(sel);
		service(function(r){getModels(year,make,vModel,r)},"FetchModels",year,make);
		return fillSELECT(sel,loading,"");
	}
	if( showSOAPError(ret) )return;
	var values = getReturnValues(ret);
	fillSELECT(sel,values);
	if( ret.value.length == 1 )model = values[0];
	if( model )Model=model;
}
function getSeries(year,make,model,series,ret){
	var sel = selSeries;
	if( !isCB(ret) ){
		var vSeries = selectedValue(sel);
		service(function(r){getSeries(year,make,model,vSeries,r)},"FetchSeries",year,make,model);
		return fillSELECT(sel,loading,"");
	}
	if( showSOAPError(ret) )return;
	var values = getReturnValues(ret);
	fillSELECT(sel,values);
	if( ret.value.length == 1 )series = values[0];
	if( series )Series=series;
}
function getBodyStyles(year,make,model,series,bodyStyle,ret){
	var sel = selBodyStyle;
	if( !isCB(ret) ){
		var vBody = selectedValue(sel);
		service(function(r){getBodyStyles(year,make,model,series,vBody,r)},"FetchBodyStyles",year,make,model,series);
		return fillSELECT(sel,loading,"");
	}
	if( showSOAPError(ret) )return;
	if( ret.value.length > 1 )ret.value.shift();
	fillSELECT(sel,ret.value);
	BodyStyle = bodyStyle || BodyStyle;
}
function getAddOns(year,make,model,series,bodyStyle,ret){
	BookValue = "";
	if( arguments.length == 0 )return rdsAddOns.sourceRecordset = emptyRS();
	if( !isCB(ret) ){
		getAddOns();
		return service(function(r){getAddOns(year,make,model,series,bodyStyle,r)},"FetchAddOnsRS",year,make,model,series,bodyStyle);
	}
	if( showSOAPError(ret) )return;
	var xml = new ActiveXObject("Msxml2.DOMDocument");
	xml.loadXML(ret.value);
	var rs = new ActiveXObject("adodb.recordset");
	rs.Open(xml);
	rdsAddOns.sourceRecordset = rs;
	getBookValues(year,make,model,series,bodyStyle,AddOns,"FL",0);
	return;
}
function getBookValues(year,make,model,series,bodyStyle,addOns,state,mileage,ret){
	if( arguments.length != 1 ){
		BookValue = "";
		return service(function(r){getBookValues(r)},"FetchBookValue",year,make,model,series,bodyStyle,addOns,state,mileage);
	}
	var ret = year;
	if( showSOAPError(ret) )return;
	var rs = initRS(new Array("Checked",11,1),"Label","Value");
	var a = ret.value[0];
	BookValue = formatMoney(a[a.length-3]);
	return;
	for( var i=a.length-3; i < a.length; i++ ){
		rs.AddNew("Label",aLabels[i]);
		rs("Value").Value = a[i];
		if( rs.absolutePosition == 1 )rs("Checked").value = true;
	}
	rdsBVs.sourceRecordset = rs;
}
// ***** CarInfo event handlers
function addOn_Changed(chk){
	var ev = createEventObject();
	ev.addOnIndex = chk.recordNumber;
	var ret = addonchange.fire(ev);
	getBookValues(Year,Make,Model,Series,BodyStyle,AddOns,State,Mileage);
}
// ***** Helper functions
function showBB(show){
	var bShow = arguments.length ? eval(show) : !_showBB();
	if( bShow )getYears();
	_showBB(bShow);
	/// Locals
	function _showBB(bShow){
		if( !defined(bShow) )return tblBB.style.display != "none";
		tblBB.style.display = bShow ? "" : "none";
		imgBB.src = imgBB.src.replace(/(?:plus|minus)_big.gif/i,(bShow?"minus":"plus")+"_big.gif");
	}
}
function isCB(ret){return ret && defined(ret.error) && (ret.raw || ret.errorDetail);}
function fillSELECT(sel,texts,values){
	var value = selectedValue(sel);
	sel.length = 0;
	for( var i=0; i < texts.length; i++ )
		sel[sel.length] = new Option(texts[i],defined(values)?values[i]:texts[i]);
	sel.parentElement.parentElement.style.display = !sel.length || sel.length == 1 && sel[0].value == "?" ? "none" : "block";
	selectByValue(sel,value);
	//if( arguments.callee == arguments.callee.caller )return;
	return;
}
function clearSelections(sel){
	switch(sel.id){
		case 'selYear'		 : Make = empty;
		case 'selMake'		 : Model = empty;
		case 'selModel'		 : Series = empty;
		case 'selSeries'	 : BodyStyle = empty;
		case 'selBodyStyle': getAddOns();
	}
}
function appendElement(root,name,attributes){
	var node = root.appendChild((root.ownerDocument||root).createElement(name));
	var a;
	for( a in (attributes||{}) )
			node.setAttribute(a,attributes[a]);
	return node;
}
function selectByValue(sel,value){
	for( var i=0; value != null && i < sel.length; i++ )
		if( value.match(new RegExp("("+sel[i].value+"|"+sel[i].text+")","i")) )return sel[i].selected = true;
	//sel.selectedIndex = -1;
	return false;
}
function selectedValue(sel){return sel.length == 0 || sel.length > 1 && sel.value == "?" ? null : sel.value;}
function isSelected(sel){return sel.length == 1 || (sel.length > 0 && sel.value != "?");}
function isSelectEmpty(sel){return sel.length == 0;}
function isObject(v){return typeof(v) == "object";}
function defined(v){return typeof(v)!='undefined';}
function getReturnValues(ret){
	if( ret.value.length == 2 )ret.value.shift();
	return ret.value;
}
function initRS(){
	var rs = new ActiveXObject("adodb.recordset");
	for( var i=0; i < arguments.length; i++ )
		rs.Fields.Append(arguments[i][0]||arguments[i],arguments[i][1]||200,arguments[i][2]||256);
	if( rs.Fields.Count == 0 )rs.Fields.Append("Field1",200,256);
	rs.Open();
	return rs;
}
function emptyRS(){
	if( !emptyRS.rs )emptyRS.rs = initRS();
	return emptyRS.rs;
}
function formatMoney(n){
	return (n.toString().match(/(\d*)[.]?/)[1]||"0").match(/\d/g).reverse().join('').match(/\d{1,3}/g).join(',').match(/./g).reverse().join('')+((n.toString().match(/[.]+\d+/)||{})[0]||"");
}
